/**
 * Copyright (c) 2010 - 2012, Sina Inc. All rights reserved.
 * @fileoverview Sina  新建案例里的产品
 * @author bowen liu | liubowen082@163.com
 * @version 1.1 | 2013-4-13
 * @example
 * 
 */


$require('cpt.uploadPic');
$require('cpt.edProPic');
$require('mod.cAlert');
$require('mod.cPrompt');
//$require('mod.uploadFlv');
LJS.define('cpt.createProList',function(ljs){
    var uploadPic = ljs.cpt.uploadPic,_l=null;
	//var uploadPic = ljs.mod.uploadFlv;
    var createProList = {
        arrList : [],
        isPlayIndex : null,
        init:function(el){
            var that = this;
			that.el = el; // 图片点击上传按钮
				that.uploadPic = new uploadPic({uploadBox:el});
				//that.uploadPic = new uploadPic({box:el,objectName:'window.__uploadFlv'});
				//window.__uploadFlv = that.uploadPic;
				//that.uploadPic.create();
				$(that.uploadPic).on('uploadSuc', function(evt, data){
				   var val = ljs.cpt.edProPic.setSelectCon()
				   $.each(data.content,function(i,a){
												a.pros = [];
												a.space = val;
											});
				   that.createList(data.content);
				   
			    });
			    $(that.uploadPic).on('uploadErr', function(evt, data){
				   ljs.mod.cAlert({txt:data.msg})
			    });
				
			
				
				
                this.addEvent();
                ljs.cpt.edProPic.init();
                $(ljs.cpt.edProPic).on('changeData',function(evt,data){
                    that.arrList[that.isPlayIndex] = data;
                })
				$(ljs.cpt.edProPic).on('playNext',function(evt,data){
					if(that.isPlayIndex==null) return;
                    switch(data){
						case 'pro':
						 if(that.isPlayIndex-1<0){
							 return
							 }
					     $('#editPicList li').eq(that.isPlayIndex-1).click();
						break;
						case 'next':
							if(that.isPlayIndex+1>=that.arrList.length){
							 return
							 }
					     	$('#editPicList li').eq(that.isPlayIndex+1).click();
						break;
					}
                })
                
            },
        createList : function(data){
			if(!data) return
            var str = '',that = this,index = that.arrList.length;
            $.each(data,function(i,a){
				str +='<li id="'+(index+i)+'" picId="'+a.id+'" ><a href="javascript:void(0)"><img src="'+a.url_small+'"/><span class="at03" style="display:none">编辑完成!</span></a></li>';
				data.pros = data.pros || [];
            })
            $('#editPicList').append(str);
			var len = that.arrList.length;
            $.merge(that.arrList,data);
			if(len==0){
				$('#editPicList li:first').click();
				}else{
					$('#editPicList li').eq(len).click();
				}
			$('#casePicNum').html(that.arrList.length);
			
			// 如果超过30张
			if(that.arrList.length>=30){
				$(that.el).attr('isfulled','true')
			}
        },
        addEvent:function(){
            var that = this;
            $('#editPicList').on('click','li',function(){
                var index = parseInt($(this).attr('id'));
                var data = that.arrList[index]
                ljs.cpt.edProPic.setData(data,index+1);
				
				
				$('#editPicList a').removeClass('btn_off')
				$(this).find('a').addClass('btn_off');
                that.isPlayIndex = index;
            });
            // 删除图片
            
            $('#editProDel').on('click',function(){
			    if(that.isPlayIndex===null) return
				//var index = $('#editPicList li').eq(that.isPlayIndex)
				if(!that.arrList[that.isPlayIndex]) return;
				if(_l) return
				var del = function(){
					
					 var id = that.arrList[that.isPlayIndex].id;
				
					  $('#editPicList li').eq(that.isPlayIndex).remove();
					  ljs.cpt.edProPic.delData();
					  // 更新数据
					  that.arrList.splice(that.isPlayIndex,1);
					  $.each($('#editPicList li'),function(i,a){
								$(a).attr('id',i)
							})
					  $('#casePicNum').html(that.arrList.length);
					  $('#editPicList li').eq(0).click();
					   // 如果超过30张
					if(that.arrList.length<30){
						$(that.el).attr('isfulled','false')
					}
				}
				
				_l = ljs.mod.cPrompt({
					txt : '确认删除此项',
					fun : function(){
						del()
						_l.del();
						_l = null;
					}
				});
				$(_l).on('onDel',function(){
					_l = null;
				})
            })
        },
        getData : function(){
            return this.arrList;
        }
        
    }
    return createProList;

});
